﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace WindowsFormsApplication1
{
    public partial class frmDTXetTN : Form
    {
        DataSet ds_duocxet = new DataSet();
        DataSet ds_daduocxet = new DataSet();
        DataSet ds_chuaxet = new DataSet();
        DataSet ds_tamngung = new DataSet();
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");

        public frmDTXetTN()
        {
            InitializeComponent();
            xtraTabControl1.Visible = false;
        }

        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            return ds;
        }

        private void frmDTXetTN_Load(object sender, EventArgs e)
        {
            DataSet ds = FillDataset("select MalopID, Tenlop from T03_Lop_hoc where Khoaso = 1");
            lkuMalop.Properties.DataSource = ds.Tables[0];                
        }

        public void DSSV_ChuaxetTN()
        {
            ds_chuaxet = FillDataset("exec PC_DSSVChuaxet '" + lkuMalop.Text + "'");
            gridControl3.DataSource = ds_chuaxet.Tables[0];
        }

        public void DSSV_DuocxetTN()
        {
            ds_duocxet = FillDataset("exec PC_DSSVduocxetTN '" + lkuMalop.Text + "'");
            gridControl1.DataSource = ds_duocxet.Tables[0];
        }

        public void DSSV_DaduocxetTN()
        {
            ds_daduocxet = FillDataset("exec PC_DSSVDaxet '" + lkuMalop.Text + "'");
            gridControl4.DataSource = ds_daduocxet.Tables[0];
        }

        public void DSSV_TamNgungTN()
        {
            ds_tamngung = FillDataset("exec PC_DSSVduocxetTN '" + lkuMalop.Text + "'");
            gridControl2.DataSource = ds_tamngung.Tables[0].Clone();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                xtraTabControl1.Visible = true;
                SetUpGrid(gridControl1);
                SetUpGrid(gridControl2);
                DSSV_DuocxetTN();
                DSSV_TamNgungTN();
                DSSV_ChuaxetTN();
                DSSV_DaduocxetTN();
            }
            else
                MessageBox.Show("Bạn chưa nhập dữ liệu cần thiết", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }


        //Thuc hien keo tha
#region
        public void SetUpGrid(GridControl grid)
        {
            grid.AllowDrop = true;
            grid.DragOver += new System.Windows.Forms.DragEventHandler(grid_DragOver);
            grid.DragDrop += new System.Windows.Forms.DragEventHandler(grid_DragDrop);
            GridView view = grid.MainView as GridView;
            view.OptionsBehavior.Editable = false;
            view.MouseMove += new System.Windows.Forms.MouseEventHandler(view_MouseMove);
            view.MouseDown += new System.Windows.Forms.MouseEventHandler(view_MouseDown);
        }

        GridHitInfo downHitInfo = null;

        private void view_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            GridView view = sender as GridView;
            downHitInfo = null;
            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
            if (Control.ModifierKeys != Keys.None) return;
            if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0)
                downHitInfo = hitInfo;
        }

        private void view_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X -
dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);

                if (!dragRect.Contains(new Point(e.X, e.Y)))
                {
                    DataRow row = view.GetDataRow(downHitInfo.RowHandle);
                    view.GridControl.DoDragDrop(row, DragDropEffects.Move);
                    downHitInfo = null;
                    DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;
                }
            }
        }

        private void grid_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(DataRow)))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }

        private void grid_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            GridControl grid = sender as GridControl;
            DataTable table = grid.DataSource as DataTable;
            DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
            if (row != null && table != null && row.Table != table)
            {
                table.ImportRow(row);
                row.Delete();
            }
        }

#endregion


        //Ket chuyen DSSV du dieu kien xet TN dot nay toi DSSV da duoc xet TN
        private void btnKetChuyen_Click(object sender, EventArgs e)
        {
            connection.Open();
            List<string> Masv = new List<string>();
            DataView view = new DataView(ds_duocxet.Tables[0], null, null, DataViewRowState.CurrentRows);
            DataTable table = view.ToTable();
            try
            {
                foreach (DataRow datarow in table.Rows)
                {
                    Masv.Add(Convert.ToString(datarow["MasvID"]));
                }
                foreach (string masvid in Masv)
                {
                    view.Delete(0);
                    string update = "exec [PC_DSSVDaxet_Update] N'" + masvid + "'";
                    SqlCommand cmd = new SqlCommand(update, connection);
                    //cmd.Connection.Open();
                    int i = cmd.ExecuteNonQuery();                
                }
                //DSSV_DuocxetTN();
                //DSSV_TamNgungTN();
                DSSV_ChuaxetTN();
                DSSV_DaduocxetTN();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            connection.Close();

        }

        private void btnInDSduDKxetTn_Click(object sender, EventArgs e)
        {
            rptDSSVDuDkDuocxetTN rpt = new rptDSSVDuDkDuocxetTN();
            rpt.DataSource = FillDataset("exec PC_DSSVduocxetTN '" + lkuMalop.Text + "'");
            rpt.BindData();
            rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
            rpt.ShowPreview();
        }

        private void btnInDSdaxetTN_Click(object sender, EventArgs e)
        {
            rptDSSVDaxetTN rpt = new rptDSSVDaxetTN();
            rpt.DataSource = FillDataset("exec PC_DSSVDaxet '" + lkuMalop.Text + "'");
            rpt.BindData();
            rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
            rpt.ShowPreview();
        }
    }
}
